package com.handydata.antifraud.rule.funcs;

import com.handydata.antifraud.common.constant.AlarmInfoCode;
import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.common.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 计算城市级别
 */
@Slf4j
@Component
public class CalcCityLevel extends CalculateAdapter {
    private int DEFAULT_CITY_LEVEL = 3;
    @Autowired
    private RedisUtil redisUtil;

    private static final String cityLevelKey = AlarmInfoCode.REDIS_GENERAL_DICT.getCode() + "_" + DictInfoCode.DICT_CITY_LEVEL.getCode() + "_";

    @Override
    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO) {

        String phoneCity=getCity(riskInputTO.getPhoneCity());
        String homeCity=riskInputTO.getProvince()+riskInputTO.getCity();
        String relationPhoneCity=getCity(riskInputTO.getRelationPhoneCity());
        String idCardCity=removeCountry(riskInputTO.getArea());

        log.info("start doCalc CalcCityLevel phoneCity {} homeCity{} relationPhoneCity{} idCardCity{}",phoneCity,homeCity,relationPhoneCity,idCardCity);

        int phoneCityLevel = getLevel(phoneCity, "手机号城市");
        int homeCityLevel = getLevel(homeCity, "家庭住址城市");
        int idCardCityLevel = getLevel(idCardCity, "身份证城市");
        int relationPhoneCityLevel = getLevel(relationPhoneCity, "联系人手机号城市");

        Map<Integer, List<Integer>> countMap = buildCountMap();
        count(countMap, phoneCityLevel);
        count(countMap, homeCityLevel);
        count(countMap, idCardCityLevel);
        count(countMap, relationPhoneCityLevel);

        log.info("finish  CalcCityLevel phoneCityLevel {} homeCityLevel{} idCardCityLevel{} relationPhoneCityLevel{}",phoneCityLevel,homeCityLevel,idCardCityLevel,relationPhoneCityLevel);


        riskMiddleTO.setPhoneCityLevel(phoneCityLevel);
        riskMiddleTO.setCity3(countMap.get(3).size());
    }

    private int getLevel(String city, String target) {
        if(city==null||city.length()<=0){
            log.info("city 为空，默认返回3");
            return 3;
        }
        city=city.replace("省","").replace("市","");
        Optional<Object> optional = Optional.ofNullable(redisUtil.get(cityLevelKey+ StringUtils.trimToEmpty(city)));
        if(!optional.isPresent()) {
            log.info("计算的目标城市:{}，城市名:{}, 未找到对应的城市级别, 设置为默认级别:{}", target, city, DEFAULT_CITY_LEVEL);
        }
        return Integer.valueOf(optional.orElse(DEFAULT_CITY_LEVEL).toString());
    }

    private  String removeCountry(String area){
        if(area!=null){
            int index=area.indexOf("市");
            if(index!=-1){
                return  area.substring(0,index);
            }
        }
        return area;
    }
    private String getCity(String area) {
        if(area!=null){
           return area.replace(",","");
        }
        return "";
    }

    private Map<Integer, List<Integer>> buildCountMap() {
        Map<Integer, List<Integer>> cityLevelCountMap = new HashMap<>();
        cityLevelCountMap.put(1, new ArrayList<>());
        cityLevelCountMap.put(2, new ArrayList<>());
        cityLevelCountMap.put(3, new ArrayList<>());
        cityLevelCountMap.put(4, new ArrayList<>());
        cityLevelCountMap.put(5, new ArrayList<>());

        return cityLevelCountMap;
    }
    private void count(Map<Integer, List<Integer>> map, int level) {
        map.get(level).add(1);
    }

}
